/*
 * FavourableManager.java
 * business
 *
 * Created by ChenTao on 2016/10/28.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.manager;

import com.google.gson.Gson;
import com.yidiandao.business.data.bean.FavourableInfo;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.utils.GsonProvider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 订单结算是赠品或加价购活动的Manager
 */
public class GiftManager {

    /**
     * 赠品活动.针对当前结算的订单
     */
    private List<FavourableInfo> mGiftFavourableInfoList = new ArrayList<>();

    private GiftManager() {
    }

    public static GiftManager getInstance() {
        return Singleton.instance;
    }

    /**
     * 清空赠品、加价购活动列表
     */
    public void clearAllFavourableInfoList() {
        mGiftFavourableInfoList.clear();
    }

    /**
     * 添加当前结算订单的赠品活动
     */
    public void addGiftFavourableInfo(FavourableInfo orderFavourableInfo) {
        // 移除修的活动信息，更新为信息的活动信息
        //        if (mGiftFavourableInfoList.contains(orderFavourableInfo)) {
        //            mGiftFavourableInfoList.remove(orderFavourableInfo);
        //        }
        mGiftFavourableInfoList.add(orderFavourableInfo);
    }

    /**
     * 通过actId获取活动信息对象
     */
    public FavourableInfo getFavourableInfoByActId(String actId) {
        for (FavourableInfo info : mGiftFavourableInfoList) {
            if (info.getActId().equals(actId)) {
                return info;
            }
        }
        return null;
    }

    /**
     * 获取当前赠品活动选中的赠品
     */
    private Goods getChooseGiftOfFavourableInfo(FavourableInfo favourableInfo) {
        return favourableInfo.getChooseGift();
    }

    /**
     * 更新赠品信息
     */
    public void updateGiftInfo(FavourableInfo giftInfo) {
        Iterator<FavourableInfo> iterator = mGiftFavourableInfoList.iterator();
        boolean hasInfo = false;
        while (iterator.hasNext()) {
            FavourableInfo next = iterator.next();
            if (next.equals(giftInfo)) {
                hasInfo = true;
                iterator.remove();
                break;
            }
        }

        if (hasInfo) {
            mGiftFavourableInfoList.add(giftInfo);
        }
    }

    /**
     * 获取结算的赠品列表的json
     */
    public String getBalanceGiftListJson() {
        List<FavourableInfo> chooseFavourableInfoList = new ArrayList<>();
        for (FavourableInfo info : mGiftFavourableInfoList) {
            Goods chooseGift = getChooseGiftOfFavourableInfo(info);
            // 如果满赠、加价购活动有选择的赠品，并且赠品活动所属商品已经选中
            if (chooseGift != null && info.getBelongGoods().isSelected()) {
                info.setGoodsId(chooseGift.getGoodsId());
                chooseFavourableInfoList.add(info);
            }
        }
        // 只生成有expose注释的文件
        Gson gson = GsonProvider.getExposeGson();
        return gson.toJson(chooseFavourableInfoList);
    }

    /**
     * 返回所有的选择的赠品/加价购的价格
     */
    float getAllSelectedGiftsPrice() {

        float totalGiftPrice = 0;

        for (FavourableInfo favourableInfo : mGiftFavourableInfoList) {
            Goods chooseGift = favourableInfo.getChooseGift();
            // 当前满赠、加价购活动有选择赠品，并且所属商品已经选择
            if (chooseGift != null && favourableInfo.getBelongGoods().isSelected()) {
                totalGiftPrice += chooseGift.getGoodsPrice() * chooseGift.getGoodsNumber();
            }
        }

        return totalGiftPrice;
    }

    /**
     * 获取所有选择赠品的数量
     */
    int getAllSelectedGiftCount() {
        int totalCount = 0;
        for (FavourableInfo favourableInfo : mGiftFavourableInfoList) {
            Goods chooseGift = favourableInfo.getChooseGift();
            // 如果当前满赠、加价购活动有选择赠品，并且该活动所属商品已经选中
            if (chooseGift != null && favourableInfo.getBelongGoods().isSelected()) {
                totalCount += chooseGift.getGoodsNumber();
            }
        }
        return totalCount;
    }

    private static class Singleton {
        private static final GiftManager instance = new GiftManager();
    }
}
